package com.hl.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hl.mapper.ShoppingAddressMapper;
import com.hl.domain.ShoppingAddress;
import com.hl.service.IShoppingAddressService;
import java.util.ArrayList;

/**
 * 收货地址Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-12
 */
@Service
public class ShoppingAddressServiceImpl implements IShoppingAddressService 
{
    @Autowired
    private ShoppingAddressMapper shoppingAddressMapper;

    /**
     * 查询收货地址
     * 
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public ShoppingAddress selectShoppingAddressById(Integer id)
    {
        return shoppingAddressMapper.selectShoppingAddressById(id);
    }
    /**
     * 查询会员收货地址列表
     *
     * @param memberId 会员id
     * @return 收货地址集合
     */
    @Override
    public List<ShoppingAddress> selectShoppingAddressByMemberId(Integer memberId)
    {
        return shoppingAddressMapper.selectShoppingAddressByMemberId(memberId);
    }
    /**
     * 查询收货地址列表
     * 
     * @param shoppingAddress 收货地址
     * @return 收货地址
     */
    @Override
    public List<ShoppingAddress> selectShoppingAddressList(ShoppingAddress shoppingAddress)
    {
        // 安全验证：确保memberId不为空
        if (shoppingAddress == null || shoppingAddress.getMemberId() == null) {
            return new ArrayList<>();
        }
        return shoppingAddressMapper.selectShoppingAddressList(shoppingAddress);
    }

    /**
     * 新增收货地址
     * 
     * @param shoppingAddress 收货地址
     * @return 结果
     */
    @Transactional
    @Override
    public int insertShoppingAddress(ShoppingAddress shoppingAddress)
    {
        shoppingAddress.setCreateTime(DateUtils.getNowDate());
        shoppingAddress.setUpdateTime(DateUtils.getNowDate());
        
        // 如果设置为默认地址，先取消当前用户其他地址的默认状态
        if (shoppingAddress.getIsDefault() != null && shoppingAddress.getIsDefault() == 1) {
            shoppingAddressMapper.clearDefaultAddress(shoppingAddress.getMemberId());
        }
        
        return shoppingAddressMapper.insertShoppingAddress(shoppingAddress);
    }

    /**
     * 修改收货地址
     * 
     * @param shoppingAddress 收货地址
     * @return 结果
     */
    @Transactional
    @Override
    public int updateShoppingAddress(ShoppingAddress shoppingAddress)
    {
        shoppingAddress.setUpdateTime(DateUtils.getNowDate());
        
        // 如果设置为默认地址，先取消当前用户其他地址的默认状态
        if (shoppingAddress.getIsDefault() == 1) {
            shoppingAddressMapper.clearDefaultAddress(shoppingAddress.getMemberId());
        }
        
        return shoppingAddressMapper.updateShoppingAddress(shoppingAddress);
    }

    /**
     * 批量删除收货地址
     * 
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteShoppingAddressByIds(Integer[] ids)
    {
        return shoppingAddressMapper.deleteShoppingAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     * 
     * @param id 收货地址主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteShoppingAddressById(Integer id)
    {
        return shoppingAddressMapper.deleteShoppingAddressById(id);
    }


}
